package main;
import javax.swing.JViewport;

import advance.GameMap;

import tool.dialog.ErrorDialog;
import controller.Controller;
import controller.NullStaticObjectException;
import controller.gui.DefaultCancelButtonController;
import controller.gui.InsertionDisplayController;
import controller.gui.backgroundmenu.BackgroundConceptBrowseController;
import controller.gui.backgroundmenu.BackgroundConceptSelectionBoxController;
import controller.gui.backgroundmenu.BackgroundOKButtonController;
import controller.gui.backgroundmenu.BackgroundRendererBrowseController;
import controller.gui.backgroundmenu.BackgroundRendererSelectionBoxController;
import controller.gui.informationpanel.MapInformationController;
import controller.gui.mainmenu.BackgroundWizardController;
import controller.gui.mainmenu.DeleteMapElementController;
import controller.gui.mainmenu.DownCurrentStrataWizardController;
import controller.gui.mainmenu.MapLayerWizardController;
import controller.gui.mainmenu.MapObjectWizardController;
import controller.gui.mainmenu.MapPropertiesWizardController;
import controller.gui.mainmenu.MapStrataWizardController;
import controller.gui.mainmenu.UpCurrentStrataWizardController;
import controller.gui.maplayermenu.MapLayerConceptBrowseController;
import controller.gui.maplayermenu.MapLayerConceptSelectionBoxController;
import controller.gui.maplayermenu.MapLayerOKController;
import controller.gui.maplayermenu.MapLayerRendererBrowseController;
import controller.gui.maplayermenu.MapLayerRendererSelectionBoxController;
import controller.gui.mapmenu.MapPropertiesOKController;
import controller.gui.mapobjectmenu.MapObjectConceptBrowseController;
import controller.gui.mapobjectmenu.MapObjectConceptSelectionBoxController;
import controller.gui.mapobjectmenu.MapObjectOKButtonController;
import controller.gui.mapobjectmenu.MapObjectRendererBrowseController;
import controller.gui.mapobjectmenu.MapObjectRendererSelectionBoxController;
import controller.gui.mapstratamenu.MapStrataAddAfterCurrentController;
import controller.gui.mapstratamenu.MapStrataAddCeilingController;
import controller.gui.mapstratamenu.MapStrataAddFloorController;
import controller.gui.mapstratamenu.MapStrataCancelButtonController;
import controller.gui.mapstratamenu.MapStrataDeleteCurrentController;
import controller.gui.menubar.ExitMapController;
import controller.gui.menubar.NewMapController;
import controller.gui.menubar.OpenMapController;
import controller.gui.menubar.SaveMapAsController;
import controller.gui.menubar.SaveMapController;
import errors.DimensionNotAcceptableException;
import gui.MapEditorFrame;


public class MapEditor {
	
	/** Quel particolare JFrame utilizzato per riprodurre l'aspetto grafico dell'Editor. */
	private static MapEditorFrame frame;
	private static NewMapController newMapController;
	private static OpenMapController openMapController;
	private static SaveMapController saveMapController;
	private static SaveMapAsController saveMapAsController;
	private static ExitMapController exitMapController;
	
	private static InsertionDisplayController displayController;
	
	private static BackgroundConceptSelectionBoxController backgroundConceptSelectionBoxController;
	private static BackgroundRendererSelectionBoxController backgroundRendererSelectionBoxController;
	
	private static MapLayerConceptSelectionBoxController maplayerConceptSelectionBoxController;
	private static MapLayerRendererSelectionBoxController maplayerRendererSelectionBoxController;
	
	private static MapObjectConceptSelectionBoxController mapobjectConceptSelectionBoxController;
	private static MapObjectRendererSelectionBoxController mapobjectRendererSelectionBoxController;

	/** Altro non e' che la funzione main dell'Editor di mappe.
	 * @param args 
	 * @throws NullStaticObjectException 
	 * @throws DimensionNotAcceptableException */
	public static void main(String[] args) throws NullStaticObjectException, DimensionNotAcceptableException {
		//controllo che il computer locale rispetti i requisiti del software
		try{
			if (!MapEditor.controlVersion()){
				new ErrorDialog(null,"java version error","java version not correct! required at least 1.7").setVisible(true);
				return;
			}
			//creazione dell'interfaccia grafica
			frame = new MapEditorFrame("MD's Game Maps Editor");
			//cablaggio statico delle variabili importanti
			Controller.setLog(frame.getLog());
			//creazione della mappa standard
			Controller.setCurrentmap(new GameMap("",30,15,15));
			Controller.setDrawingComponent(frame.getDisplay());
			//creazione dei controller
				//****************** controller della menu bar ***************************
				newMapController=new NewMapController(frame,frame.getJMenuBar().getNewMap());
				openMapController=new OpenMapController(frame,frame.getJMenuBar().getOpen());
				saveMapController=new SaveMapController(frame,frame.getJMenuBar().getSave());
				saveMapAsController=new SaveMapAsController(frame,frame.getJMenuBar().getSaveAs());
				exitMapController=new ExitMapController(frame,frame.getJMenuBar().getExit());
				//****************** controller della Central View ***********************
				new MapInformationController(frame.getCentralView().getStrataOverviewPanel(),frame.getDisplay(),frame.getCentralView().getxLabel(),frame.getCentralView().getyLabel());
				displayController=new InsertionDisplayController(frame, frame.getDisplay(),frame.getBackgroundSecondaryMenu().getAddToMapButton(),frame.getMapLayerSecondaryMenu().getAddToMapButton(),frame.getMapObjectSecondaryMenu().getAddToMapButton());
				
				new MapPropertiesWizardController(frame.getPrimaryMenu().getMap_button(),frame.getMapSecondaryMenu());
				new MapPropertiesOKController(frame.getMapSecondaryMenu().getOkButton(), frame.getMapSecondaryMenu());
				new DefaultCancelButtonController(frame.getMapSecondaryMenu().getCancelButton(),frame.getMapSecondaryMenu(),frame.getDisplay());
				
				new BackgroundWizardController(frame.getPrimaryMenu().getBkg_button(),frame.getBackgroundSecondaryMenu());
				new BackgroundOKButtonController(frame.getBackgroundSecondaryMenu().getOkButton(),frame.getBackgroundSecondaryMenu());
				new DefaultCancelButtonController(frame.getBackgroundSecondaryMenu().getCancelButton(),frame.getBackgroundSecondaryMenu(),frame.getDisplay());
				backgroundConceptSelectionBoxController=new BackgroundConceptSelectionBoxController(frame,displayController,frame.getBackgroundSecondaryMenu().getChooseConceptWindow());
				backgroundRendererSelectionBoxController=new BackgroundRendererSelectionBoxController(frame,displayController,frame.getBackgroundSecondaryMenu().getChooseRendererWindow());
				new BackgroundConceptBrowseController(frame,frame.getBackgroundSecondaryMenu().getBrowseConceptButton(), backgroundConceptSelectionBoxController);
				new BackgroundRendererBrowseController(frame, frame.getBackgroundSecondaryMenu().getBrowseRendererButton(), backgroundRendererSelectionBoxController);
				
				new MapLayerWizardController(frame.getPrimaryMenu().getLyr_button(), frame.getMapLayerSecondaryMenu());
				new MapLayerOKController(frame.getMapLayerSecondaryMenu().getOkButton(), frame.getMapLayerSecondaryMenu());
				new DefaultCancelButtonController(frame.getMapLayerSecondaryMenu().getCancelButton(),frame.getMapLayerSecondaryMenu(),frame.getDisplay());
				maplayerConceptSelectionBoxController=new MapLayerConceptSelectionBoxController(frame,displayController, frame.getMapLayerSecondaryMenu().getChooseConceptWindow());
				maplayerRendererSelectionBoxController=new MapLayerRendererSelectionBoxController(frame,displayController, frame.getMapLayerSecondaryMenu().getChooseRendererWindow());
				new MapLayerConceptBrowseController(frame, frame.getMapLayerSecondaryMenu().getBrowseConceptButton(),maplayerConceptSelectionBoxController);
				new MapLayerRendererBrowseController(frame, frame.getMapLayerSecondaryMenu().getBrowseRendererButton(),maplayerRendererSelectionBoxController);
				
				new MapObjectWizardController(frame.getPrimaryMenu().getObj_button(),frame.getMapObjectSecondaryMenu());
				new MapObjectOKButtonController(frame.getMapObjectSecondaryMenu().getOkButton(), frame.getMapObjectSecondaryMenu());
				new DefaultCancelButtonController(frame.getMapObjectSecondaryMenu().getCancelButton(),frame.getMapObjectSecondaryMenu(),frame.getDisplay());
				mapobjectConceptSelectionBoxController=new MapObjectConceptSelectionBoxController(frame, displayController,frame.getMapObjectSecondaryMenu().getChooseConceptWindow());
				mapobjectRendererSelectionBoxController=new MapObjectRendererSelectionBoxController(frame, displayController,frame.getMapObjectSecondaryMenu().getChooseRendererWindow());
				new MapObjectConceptBrowseController(frame, frame.getMapObjectSecondaryMenu().getBrowseConceptButton(),mapobjectConceptSelectionBoxController);
				new MapObjectRendererBrowseController(frame, frame.getMapObjectSecondaryMenu().getBrowseRendererButton(), mapobjectRendererSelectionBoxController);
				
				new MapStrataWizardController(frame.getPrimaryMenu().getStr_button(),frame.getStrataSecondaryMenu());
				new MapStrataCancelButtonController(frame.getStrataSecondaryMenu().getCancelButton(),frame.getStrataSecondaryMenu());
				new MapStrataAddAfterCurrentController(frame.getStrataSecondaryMenu().getAddStratumAfterCurrent(),frame.getStrataSecondaryMenu());
				new MapStrataAddFloorController(frame.getStrataSecondaryMenu().getAddStratumAtBeginning(),frame.getStrataSecondaryMenu());
				new MapStrataAddCeilingController(frame.getStrataSecondaryMenu().getAddStratumAtEnd(),frame.getStrataSecondaryMenu());
				new MapStrataDeleteCurrentController(frame.getStrataSecondaryMenu().getRemoveCurrentStratum(),frame.getStrataSecondaryMenu());
				
				new DownCurrentStrataWizardController(frame.getPrimaryMenu().getDwn_button(),frame.getDisplay());
				//****************** up current stratum ******************************
				new UpCurrentStrataWizardController(frame.getPrimaryMenu().getUp_button());
				//******************* delete map element ****************************
				new DeleteMapElementController(frame.getPrimaryMenu().getDel_button(),frame.getDisplay());
		}catch (Exception genericerror){
			new ErrorDialog(null,"startuperror",genericerror.getMessage());
		}
	}
	
	/**
	 * 
	 * @return TRUE se il JRE è almeno 1.7; FALSE altrimenti
	 */
	private static boolean controlVersion(){
		double javaversion=Double.parseDouble(System.getProperty("java.version").substring(0,3));
		return javaversion>=1.7;
	}

}
